/**
 * Created by Bane.Shi.
 * Copyright MoenSun
 * User: Bane.Shi
 * Blog: http://blog.fengxiaotx.com
 * Date: 2017/3/28
 * Time: 14:04
 */
'use strict';
import path from 'path';
import webpack from 'webpack';
import colors from 'colors';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import TransferWebpackPlugin from 'transfer-webpack-plugin';
import Clean from 'clean-webpack-plugin';
import args from "node-args";
const development = 'development';
const production = 'production';
const ENV = args.env || development;
let config = {
    entry:{
        main  : './src/app/main.js',
    },
    output:{
        path: path.resolve(__dirname,'dist'),
        filename:'[name].bundle.js'
    },
    resolve:{
        alias:{
        },
        modules: [path.resolve(__dirname, "src"), "node_modules"],
        extensions: ['.web.js', '.js', '.json','.jsx'],
    },
    module:{
        rules:[
            {
                test: /\.js[x]?$/,
                exclude: /node_modules/,
                use:[
                    {
                        loader: 'babel-loader' ,
                        query:{
                            presets:['es2015','react','stage-0'],
                            plugins:['transform-runtime']
                        }
                    }
                ]
            },
            {
                test: /\.css$/,
                use:ExtractTextPlugin.extract({
                    use:"css-loader"
                })
            },
            {test: /\.(png|jpg|gif)$/,use:[{loader:'url-loader'}] },
            {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, use:[{loader:'url-loader'}]},
            {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, use:[{loader:'url-loader'}]},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, use:[{loader:'url-loader'}]},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use:[{loader:'file-loader'}]},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use:[{loader:'url-loader'}]},
        ]
    },
    plugins:[
        new Clean(['dist']),
        new TransferWebpackPlugin([
/*            {from: 'images', to: 'images'},
            {from: 'vendor', to: 'vendor'}*/
        ], path.join(__dirname, 'src')),
        new webpack.ProvidePlugin({
            _ : 'lodash',
            $ : 'jquery',
            jQuery : 'jquery',
        }),
        new ExtractTextPlugin('[name].bundle.css'),
        new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' }),
        new HtmlWebpackPlugin({
            title:'测试',
            template: './src/index.ejs',
            hash: true,
            minify: {
                collapseWhitespace: true,
                collapseBooleanAttributes: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true
            }
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DefinePlugin({
            'process.env':{
                NODE_ENV:("\'"+ENV+"\'" || "\'"+development+"\'")
            }
        }),
    ],
    devtool: (ENV == production)?null: '#source-map'
}
let collection = ((pluginItems)=>{
    let {plugins} = config;
    plugins = [...plugins,...pluginItems];
    Object.assign(config,{plugins});
});
const productionPlugins = [
    new webpack.optimize.UglifyJsPlugin({
        compress:{
            warnings:false
        }
    })
];
switch (ENV){
    case development:
        break;
    case production:
        collection(productionPlugins);
        break;
    default:
        break;
}
module.exports = config;